package ngames.com.sushitv.tools;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;

import java.io.File;

/**
 * Created by Jeffrey on 2016/7/26.
 */
public class Utils {
    private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE";

    /**
     * 获取当前网络状态
     * -1:没有网络  1:wifi网络  2:wap网络  3:net网络
     */
    public final static int CMNET = 3;
    public final static int CMWAP = 2;
    public final static int WIFI = 1;
    public final static int NONEWTWORK = -1;

    /**
     * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
     * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
     * <p>
     * sdcard存在就在storage/emulated/0/Android/data/包名/cache 目录下创建
     * sdcard不存在就在/data/data/包名/cache 目录下创建
     *
     * @param context  Application context
     * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
     * @return Cache {@link File directory}
     */
    public static File getOwnCacheDirectory(Context context, String cacheDir) {
        File appCacheDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
//            appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);//在sdcard根目录创建
            appCacheDir = new File(context.getExternalCacheDir(), cacheDir);//在sdcard程序缓存目录下创建
        }
        if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
            appCacheDir = new File(context.getCacheDir(), cacheDir);//在程序安装目录下的缓存目录创建
        }
        if (!appCacheDir.exists()) {
            appCacheDir.mkdirs();
        }
        return appCacheDir;
    }

    private static boolean hasExternalStoragePermission(Context context) {
        int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
        return perm == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isNetConnect(Context context) {
        if (NONEWTWORK == getAPNType(context)) {
            return false;
        }
        return true;
    }

    public static int getAPNType(Context context) {
        int netType = NONEWTWORK;
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
                netType = CMNET;
            } else {
                netType = CMWAP;
            }
        }
        if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = WIFI;
        }
        return netType;
    }

    public static String getDeviceId(Context context) {
        return MD5.toMD5(getMAC(context) + getIMEI(context));
    }

    public static String getMAC(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String wifiMac = info.getMacAddress();
        LogUtil.d("Utils", "getMAC() " + wifiMac);
        return wifiMac;
    }

    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        LogUtil.d("Utils", "getIMEI() " + imei);
        return imei;
    }
}
